package br.com.californiahotel.client.viewControler.reserva;

import java.util.LinkedHashMap;
import java.util.List;

import br.com.californiahotel.client.architecture.viewController.BasicController;
import br.com.californiahotel.client.interfaces.AsyncCallback;
import br.com.californiahotel.client.service.ApartamentoService;
import br.com.californiahotel.client.service.ApartamentoServiceAsync;
import br.com.californiahotel.client.service.ReservaService;
import br.com.californiahotel.client.service.ReservaServiceAsync;
import br.com.californiahotel.client.viewControler.reserva.ReservaFormView.Field;
import br.com.californiahotel.shared.models.Apartamento;
import br.com.californiahotel.shared.models.Reserva;

import com.google.gwt.core.client.GWT;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.DateItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;

public class ReservaFormController extends BasicController {

	private final ReservaServiceAsync reservaService = GWT.create(ReservaService.class);

	public interface Display extends DisplayController {
		DynamicForm getForm();
	}

	public ReservaFormController(Display display) {
		super(display);
	}

	@Override
	protected void bind() {
		getItem(Field.SAVE).addClickHandler(saveButtonClickEvent());
		getItem(Field.DATE).addChangedHandler(new ChangedHandler() {

			@Override
			public void onChanged(ChangedEvent event) {
				getItem(Field.APARTAMENTO).clearValue();
				ApartamentoServiceAsync aptoService = GWT.create(ApartamentoService.class);
				aptoService.findDisponiblesByDate(((DateItem)getItem(Field.DATE)).getValueAsDate(), new AsyncCallback<List<Apartamento>>() {

					@Override
					public void onSuccess(List<Apartamento> result) {
						LinkedHashMap<String, String> hashMap = new LinkedHashMap<String, String>();
						for (Apartamento apartamento : result) {
							hashMap.put(apartamento.getNumeroApartamento().toString(), apartamento.getNumeroApartamento().toString());
						}
						getItem(Field.APARTAMENTO).setValueMap(hashMap);
					}
				});
			}
		});
	}

	private ClickHandler saveButtonClickEvent() {
		return new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				reservaService.save(new Reserva(((DateItem)getItem(Field.DATE)).getValueAsDate(),getItem(Field.HOSPEDE).getDisplayValue(),getItem(Field.APARTAMENTO).getDisplayValue()), new AsyncCallback<Boolean>() {

					@Override
					public void onSuccess(Boolean result) {
						if(result == true){
							SC.say("Reserva efetuada com sucesso");
						}else {
							SC.say("Falha");
						}
					}
				});
			}
		};
	}

	private Display getDisplay() {
		return ((Display)getDisplayController());
	}

	private FormItem getItem(Field value) {
		return getDisplay().getForm().getItem(value.getFieldName());
	}

}
